package Pages;
import javax.swing.*;

import Others.ButtonTabComponent;
import Tables.CustomerTableInterface;
import Tables.CustomerUnitPriceTableInterface;
import Tables.PromotionTableInterface;
import Tables.PersonelTableInterface;
import Tables.ProductTableInterface;
import Tables.SupplierTableInterface;
import Tables.VehicleTableInterface;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

public class BasicInformationPage extends JFrame implements ActionListener{
	JTabbedPane jtp;
	JPanel jp1;
	public static int tabNum=0;

	JButton customerButton = new JButton("Müşteri Bilgileri");
    JButton vehicleButton = new JButton("Araç Bilgileri");
    JButton routeButton = new JButton("Rut Bilgileri");
    JButton productButton = new JButton("Ürün Bilgileri");
	JButton driverButton = new JButton("Personel Bilgileri");
    JButton promotionButton = new JButton("Promosyon Bilgileri");
    JButton providerButton = new JButton("Tedarikçi Bilgileri");
    JButton specialPriceButton = new JButton("Özel Fiyat Bilgileri");
    JButton exitButton = new JButton("Exit");
    
    
    ///////////////////////////  For Time
    public JLabel show_time = new JLabel("Show Time");
    public JLabel Show_date = new JLabel("Show Date");

    DateFormat dateFormat2 = new SimpleDateFormat("h:mm:ss a");
    java.util.Date date2;
    long msec ;
    
	//////////////////////////////
    
    JLabel ResimTutucu1 = new JLabel();// for catch the image
	ImageIcon Resim1 = new ImageIcon(getClass().getResource("TemelBilgiler.png"));   
	

	CustomerTableInterface customerTable;
	VehicleTableInterface vehicleTable;
	ProductTableInterface productTable;
	PersonelTableInterface personelTable;
	CustomerUnitPriceTableInterface specialPriceTable;
	PromotionTableInterface promotionTable;
	SupplierTableInterface supplierTable;
	
    public BasicInformationPage() {
    	tabNum=0;
        
         //This will create the title you see in the upper left of the window    
        setTitle("Temel Bilgiler");  
        setSize(950,750); //set size so the user can "see" it
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        //Here we are creating the object
        jtp = new JTabbedPane(JTabbedPane.TOP);
      


        							//This creates the template on the windowed application that we will be using
        getContentPane().add(jtp);

        jp1 = new JPanel();//This will create the first tab
        
        JPanel panel1 = new JPanel();
        JPanel panel[]=new JPanel[60];
        for(int i=0;i<60;i++)
    		panel[i]=new JPanel();
        panel1.setLayout(new GridBagLayout());
        
        ResimTutucu1.setIcon(Resim1);
        addItem(panel1, ResimTutucu1, 0, 0, 5, 2, GridBagConstraints.NORTH);
       
        
        Font f= new Font("SansSerif", Font.PLAIN, 13);
        DateFormat dateFormat = new SimpleDateFormat("EEEEEEEEE: dd MMMMMMMMM, yyyy.");
        java.util.Date date = new java.util.Date();
        String dateTime = dateFormat.format(date);
        Show_date = new JLabel(dateTime);
        Show_date.setFont(f); 
        show_time= new JLabel("");
        updateDateTime();
        show_time.setFont(f);
        
        
        Box showDateTimeBox = Box.createVerticalBox();
        JLabel showDateTimeGroup = new JLabel();
        showDateTimeGroup.add(Show_date);
        showDateTimeGroup.add(show_time);
        showDateTimeBox.add(Show_date);
        showDateTimeBox.add(show_time);
        showDateTimeBox.setBorder(BorderFactory.createTitledBorder(""));
        addItem(panel1, showDateTimeBox, 0, 3, 1, 1, GridBagConstraints.CENTER);
        
        
        customerButton.setPreferredSize(new Dimension(170,40));
        vehicleButton.setPreferredSize(new Dimension(170,40));
        routeButton.setPreferredSize(new Dimension(170,40));
        productButton.setPreferredSize(new Dimension(170,40));
        driverButton.setPreferredSize(new Dimension(170,40));
        promotionButton.setPreferredSize(new Dimension(170,40));
        providerButton.setPreferredSize(new Dimension(170,40));
        specialPriceButton.setPreferredSize(new Dimension(170,40));
       
        customerButton.addActionListener(this);
        vehicleButton.addActionListener(this);
        routeButton.addActionListener(this);
        productButton.addActionListener(this);
        driverButton.addActionListener(this);
        promotionButton.addActionListener(this);
        providerButton.addActionListener(this);
        specialPriceButton.addActionListener(this);
        
		addItem(panel1,customerButton,0,4,1,1,GridBagConstraints.CENTER);
		addItem(panel1,vehicleButton,1,4,1,1,GridBagConstraints.CENTER);
		addItem(panel1,routeButton,2,4,1,1,GridBagConstraints.CENTER);
		addItem(panel1,productButton,0,5,1,1,GridBagConstraints.CENTER);
		addItem(panel1,driverButton,1,5,1,1,GridBagConstraints.CENTER);
		addItem(panel1,promotionButton,2,5,1,1,GridBagConstraints.CENTER);
		addItem(panel1,providerButton,0,6,1,1,GridBagConstraints.CENTER);
		addItem(panel1,specialPriceButton,1,6,1,1,GridBagConstraints.CENTER);
		
		for(int i=0;i<12;i++)
		{
			if(i<3){
			addItem(panel1,panel[i],3,i+4,1,1,GridBagConstraints.CENTER);
			addItem(panel1,panel[i+10],4,i+4,1,1,GridBagConstraints.CENTER);
			addItem(panel1,panel[i+20],5,i+4,1,1,GridBagConstraints.CENTER);
			}else{
				addItem(panel1,panel[i],0,i+4,1,1,GridBagConstraints.CENTER);
				addItem(panel1,panel[i+12],1,i+4,1,1,GridBagConstraints.CENTER);
				addItem(panel1,panel[i+24],2,i+4,1,1,GridBagConstraints.CENTER);
				addItem(panel1,panel[i+36],3,i+4,1,1,GridBagConstraints.CENTER);
				addItem(panel1,panel[i+48],4,i+4,1,1,GridBagConstraints.CENTER);
			}
		}
		

        jp1.add(panel1);
        //and adds the panel to the first tab

       //This adds the first to our tabbed pane object and names it
       jtp.addTab("Temel Bilgiler", jp1); 
      // initTabComponent(0);
       setVisible(true); //otherwise you won't "see" it 
    }

    private void addItem(JPanel p, JComponent c, int x, int y, int width, int height, int align) {
        GridBagConstraints gc = new GridBagConstraints();
        gc.gridx = x;
        gc.gridy = y;
        gc.gridwidth = width;
        gc.gridheight = height;
        gc.weightx = 100.0;
        gc.weighty = 100.0;
        gc.insets = new Insets(5, 5, 5, 5);
        gc.anchor = align;
        gc.fill = GridBagConstraints.NONE;
        p.add(c, gc);
      }
	private void updateDateTime()
    {
    Thread th = new Thread(new Runnable()
        {
  
		public void run()
            {
            while(true)
                {
                date2 = new java.util.Date();
                msec = date2.getTime();
               // msec += (7.5)*60*60*1000L;
                date2.setTime(msec);
                String dateTime = dateFormat2.format(date2.getTime());
                show_time.setText(dateTime);
                show_time.updateUI();
                }
            }
        });
    th.start();
    }

	@Override
	public void actionPerformed(ActionEvent event) {
		if(event.getSource()==customerButton){
			
			   ++tabNum;
			   customerTable= new CustomerTableInterface();
			   javax.swing.SwingUtilities.invokeLater(new Runnable() {
	                public void run() {
	           jtp.addTab("Müşteri Bilgileri", customerTable);
	           jtp.setSelectedComponent(customerTable);
	           initTabComponent(tabNum);
	                }
	            });    
	        
	                	
	             
			  
		}else if (event.getSource() == vehicleButton) {
			++tabNum;
        	vehicleTable=new VehicleTableInterface();
        	javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                	jtp.addTab("Araç Bilgileri", vehicleTable);
                	jtp.setSelectedComponent(vehicleTable);
                	initTabComponent(tabNum);
                }
            });    
        	 
        }else if (event.getSource() == productButton) {
        	++tabNum;
        	productTable=new ProductTableInterface();
        	javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                	jtp.addTab("Ürün Bilgileri", productTable);
                	jtp.setSelectedComponent(productTable);
                	initTabComponent(tabNum);
                }
            });
        	
        }else if (event.getSource() == driverButton) {
        	++tabNum;
        	personelTable=new PersonelTableInterface();
        	javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                	jtp.addTab("Personel Bilgileri", personelTable);
                	jtp.setSelectedComponent(personelTable);
                	initTabComponent(tabNum);
                }
            });
        }else if (event.getSource() == specialPriceButton) {
        	++tabNum;
        	specialPriceTable=new CustomerUnitPriceTableInterface();
        	javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                	jtp.addTab("Özel Fiyat Bilgileri", specialPriceTable);
                	jtp.setSelectedComponent(specialPriceTable);
                	initTabComponent(tabNum);
                }
            });
        	
        }else if (event.getSource() == promotionButton) {
        	++tabNum;
        	promotionTable=new PromotionTableInterface();
        	javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                	jtp.addTab("Promosyon Bilgileri", promotionTable);
                	jtp.setSelectedComponent(promotionTable);
                	initTabComponent(tabNum);
                }
            });
        	
        }else if (event.getSource() == providerButton) {
        	++tabNum;
        	supplierTable=new SupplierTableInterface();
        	javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                	jtp.addTab("Tedarikçi Bilgileri", supplierTable);
                	jtp.setSelectedComponent(supplierTable);
                	initTabComponent(tabNum);
                }
            });
        	
        }else if(event.getSource()==routeButton){
        	new RoutePageInterface().runTest();
       	 	//initTabComponent(tabNum);
        	
        }
	
	}
    
    public void runTest() {
    	
        //tabComponentsItem.setSelected(true);
        jtp.setTabLayoutPolicy(JTabbedPane.WRAP_TAB_LAYOUT);
        //scrollLayoutItem.setSelected(false);
        setSize(new Dimension(950, 750));
        setLocationRelativeTo(null);
        setVisible(true);
    }
    private void initTabComponent(int i) {
    		jtp.setTabComponentAt(i,new ButtonTabComponent(jtp,1));
    }  
    

}
